package com.example.chirdrenserver.Service.AdminService.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chirdrenserver.Authority.AuthorityUtils;
import com.example.chirdrenserver.Constant.MessageConstant;
import com.example.chirdrenserver.Holder.UserHolder;
import com.example.chirdrenserver.Pojo.dto.PermissionDTO;
import com.example.chirdrenserver.Service.AdminService.PermissionService;
import com.example.chirdrenserver.Service.AdminService.RolePermissionService;
import com.example.chirdrenserver.Service.AdminService.UserRoleService;
import com.example.chirdrenserver.exception.BaseException;
import com.example.chirdrenserver.mapper.PermissionMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.chirdrenserver.Pojo.entity.*;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Yu
 * @CreateTime: 2025-06-25
 * @Description: 權限管理
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RolePermissionService rolePermissionService;



    /**
     * 初始化管理界面
     *
     * @param uId
     * @return
     */
    @Override
    public boolean initMenu(Long uId) {

        List<Permission> permissions = new ArrayList<>();
        UserRole userRole = userRoleService.getOne(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, uId));
        if(BeanUtil.isEmpty(userRole)){
            throw new BaseException(MessageConstant.ROLE_NOTEXTSTS);
        }
        List<RolePermission> rolePermissions = rolePermissionService.list(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getRoleId, userRole.getRoleId()));
        if(ObjectUtil.isEmpty(rolePermissions)){
            throw new BaseException(MessageConstant.ROLE_NULL);
        }

        rolePermissions.forEach(rolePermission -> {
            Permission permission = getOne(new LambdaQueryWrapper<Permission>().eq(Permission::getPermissionId, rolePermission.getPermissionId()));
            permissions.add(permission);
        });
        Collection<String> collect = permissions.stream().map(Permission::getPermissionSign).collect(Collectors.toList());
        AuthorityUtils.setAuthority((Long) UserHolder.get(),collect);
        return true;
    }

    @Transactional
    @Override public void deleteByids(List<Integer> id) {
        for(Integer permissionId : id){
            if(!BeanUtil.isEmpty(rolePermissionService.getOne(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getPermissionId, permissionId))))
            {
              throw new BaseException("此权限集合中还有角色拥有权限,无法删除");
            }
        }
        if(!removeBatchByIds(id))
        {
            throw new BaseException("批量删除失败");
        }
    }

    @Override
    public void updateByid(Integer id, PermissionDTO permissionDTO) {
        Permission updatePermission = new Permission();
        updatePermission.setPermissionId(id);
        Permission permission = getById(id);
        BeanUtil.copyProperties(permissionDTO, updatePermission);
        updatePermission.setCreateTime(permission.getCreateTime());
        if(!updateById(updatePermission))
        {
            throw new BaseException("更新失败");
        }
    }

    @Override public void add(PermissionDTO permissionDTO) {
        Permission updatePermission = new Permission();
        updatePermission.setCreateTime(LocalDateTime.now());

        BeanUtil.copyProperties(permissionDTO, updatePermission);
        if(!save(updatePermission))
        {
            throw new BaseException("新增失败");
        }

    }
}
